package com.situ.ymall.service.impl;

import com.situ.ymall.entity.*;
import com.situ.ymall.mapper.*;
import com.situ.ymall.service.OrderService;
import com.situ.ymall.util.MD5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private ShopcartMapper cartMapper;
    @Autowired
    private OrderDetailsMapper orderDetailsMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    @Transactional
    public Order add(Integer userId, Integer[] cartIds) throws Exception{
        // 1-先判断cartIds的长度
        if (cartIds == null || cartIds.length == 0){
            throw  new Exception("订单中不包含任何商品");
        }

        List<Shopcart> carts = new ArrayList<>();
        // 2-查询购物车的信息
        for (int i = 0 ; i < cartIds.length ; i++){
            Shopcart cart = cartMapper.selectByPrimaryKey(cartIds[i]);
            carts.add(cart);

            //验证cart是否存在
            if (cart == null){
                throw new Exception("购物车信息查询失败！订单生成失败");
            }
            // 验证cart是不是你的
            if (userId != cart.getUserId()){
                throw new Exception("购物车的用户信息出错！");
            }
            // 验证商品库存是否够用
            if (cart.getCount() > cart.getGoods().getCount()){
                throw new Exception("购买的商品库存不足！！订单失败");
            }
            // 购物车的你的购买数量不能为0
            if (cart.getCount() <= 0){
                throw new Exception("购买的商品数量商品不能为0,订单生成失败");
            }
        }
        // 生成一个订单对象
        Order order = new Order();
        // UUID
        order.setId(UUID.randomUUID().toString());
        order.setUserId(userId);
        //保存到数据库中
        int result = orderMapper.insert(order);

        if (result <= 0){
            //订单保存失败
            throw new Exception("订单信息保存失败");
        }
        // 4- 遍历carts中的数据，生成订单的详情
        for (int i =0; i < carts.size(); i++){
            OrderDetails orderDetail = new OrderDetails();
            orderDetail.setOrderId(order.getId());
            orderDetail.setGoodsId(carts.get(i).getGoodsId());
            orderDetail.setCount(carts.get(i).getCount());
            orderDetail.setPrice(carts.get(i).getGoods().getPrice());
            //保存到数据库
            result = orderDetailsMapper.insert(orderDetail);
            if (result <= 0){
                throw new Exception("订单详情保存失败");
            }
        }

        // 5- 清理购物车的信息

        for (int i = 0 ; i < carts.size(); i++){
            result = cartMapper.deleteByPrimaryKey(carts.get(i).getId());
            if (result <= 0){
                throw new Exception("购物车信息清理失败!");
            }
            // 6-扣除商品的库存
            Goods goods = carts.get(i).getGoods();
            goods.setCount(goods.getCount() - carts.get(i).getCount());
            result = goodsMapper.updateByPrimaryKey(goods);
            if (result <= 0){
                throw  new Exception("商品库存扣除失败！");
            }
        }


        // 7. 查询全部的订单信息返回
        return orderMapper.selectByPrimaryKey(order.getId());
    }

    @Override
    public Order getById(String orderId) {
        return orderMapper.selectByPrimaryKey(orderId);
    }

    @Override
    public int update(Order order) {
        return orderMapper.updateByPrimaryKey(order);
    }

    @Override
    @Transactional
    public int pay(String orderId, String paypwd, Integer userId) throws Exception {
        //验证支付密码
        User user = userMapper.selectByPrimaryKey(userId);
        if (user.getPaypwd() == null){
            // 未设置支付密码
            throw new Exception("未设置支付密码");
        }
        if ( ! user.getPaypwd().equals(MD5Util.getMD5(paypwd))){
            // 密码不对
            throw new Exception("支付密码错误");
        }
        // 扣款
        //查询订单信息
        Order order = orderMapper.selectByPrimaryKey(orderId);
        BigDecimal sum = new BigDecimal(0);
        for (int i = 0; i < order.getOrderDetails().size(); i++){
            OrderDetails orderDetails = order.getOrderDetails().get(i);
           BigDecimal total = orderDetails.getPrice().multiply(new BigDecimal(orderDetails.getCount()));
           sum = sum.add(total);
        }
        if (user.getMoney().compareTo(sum) < 0){
            throw new Exception("账目余额不足");
        }
        //从USer上扣除相应的前
        user.setMoney(user.getMoney().subtract(sum));
        // 更新用户的余额
        int result = userMapper.updateByPrimaryKey(user);
        if (result <= 0){
            throw new Exception("支付失败，请联系管理员");
        }
        // 修改订单状态
        if (order.getStatus() != 0){
            // 订单不是未支付状态
            throw new Exception("订单状态错误，无法完成支付");
        }
        order.setStatus(1);
        result = orderMapper.updateByPrimaryKey(order);
        if (result <= 0){
            // 订单状态修改失败
            throw new Exception("订单修改失败，无法完成支付");
        }
        return 1;
    }

    @Override
    public List getByUserId(Integer userId) {
        return orderMapper.selectByUserId(userId);
    }

    @Override
    @Transactional
    public int clean() {
        //清理无效订单
        // 什么样的订单才是无效的，需要查出来
        // 时间   半小时之前  订单的状态是未支付的
        List<Order> orders = orderMapper.selectInvalid();
        // 一个个去清理
        // 删除订单，删除详情，库存复原
        for (Order order : orders){
            System.err.println("11111111111111111111111");
            for (OrderDetails detail : order.getOrderDetails()){
                //库存复原
                Goods goods = detail.getGoods();
                goods.setCount(goods.getCount() + detail.getCount());
                goodsMapper.updateByPrimaryKey(goods);

                // 删除详情
                orderDetailsMapper.deleteByPrimaryKey(detail.getId());

            }
            //删除当前的订单
            orderMapper.deleteByPrimaryKey(order.getId());
        }
        return 1;
    }

    @Override
    @Transactional
    public int removeByOrderId(String orderId) {
        // 1. 查询订单信息
        Order order = orderMapper.selectByPrimaryKey(orderId);
        for (OrderDetails detail : order.getOrderDetails()){
            // 2. 改库存
            Goods goods  = detail.getGoods();
            goods.setCount(goods.getCount() + detail.getCount());
            goodsMapper.updateByPrimaryKey(goods);
            // 3. 删除订单详情
            orderDetailsMapper.deleteByPrimaryKey(detail.getId());
        }
        // 4. 删除订单信息
        return orderMapper.deleteByPrimaryKey(orderId);
    }
}
